import 'package:flutter/material.dart';
import 'package:flutter_module/map/models/ni_raster_tile_layer_model.dart';
import 'package:flutter_module/viewModels/drawer_manager_viewmodel.dart';
import 'package:flutter_module/views/mapFragmentWidgets/map_title_bar.dart';
import 'package:provider/src/provider.dart';
import 'package:flutter_module/system/constant_config.dart';
import 'dart:convert' show json, jsonDecode;
import 'base_page.dart';
import 'package:provider/provider.dart';

class BaseMapSettringPage extends BasePage {
  @override
  List<Widget> createWidgets(BuildContext context) {
    final DrawerManagerViewModel read = context.read<DrawerManagerViewModel>();

    List<Widget> widgets = [];

    widgets.add(_morePicContiner(context, read));

    return widgets;
  }

  _morePicContiner(BuildContext context, DrawerManagerViewModel read) {
    return Column(
      children: [
        Row(
          children: [
            Container(
                alignment: Alignment.centerLeft,
                child: const Text(
                  "底图",
                  style:
                      TextStyle(fontSize: ConstantConfig.appDefaultTextSize1h),
                )),
          ],
        ),
        Consumer<DrawerManagerViewModel>(
          builder: (_, read, child) {
            return Column(children: _getWidget(read.layerList, read));
          },
        ),
        const SizedBox(
          height: ConstantConfig.appContentMargin,
        ),
        Row(
          children: [
            Container(
                alignment: Alignment.centerLeft,
                child: const Text(
                  "自定义底图",
                  style:
                      TextStyle(fontSize: ConstantConfig.appDefaultTextSize1h),
                )),
            const SizedBox(
              width: ConstantConfig.appContentMargin,
            ),
            Flexible(
                child: Container(
                    alignment: Alignment.centerRight,
                    child: Row(
                      children: [
                        GestureDetector(
                          child: const Icon(Icons.add),
                          onTap: () {
                            read.showOtherLayerDialog("", "");
                          },
                        ),
                        const SizedBox(
                          width: ConstantConfig.appContentMargin,
                        ),
                        Consumer<DrawerManagerViewModel>(
                          builder: (_, read, child) {
                            return GestureDetector(
                              child: Icon(
                                Icons.delete,
                                color: read.isDelLayer == true
                                    ? Colors.red
                                    : Colors.black,
                              ),
                              onTap: () {
                                read.setDelLayer();
                              },
                            );
                          },
                        )
                      ],
                    ))),
          ],
        ),
        Consumer<DrawerManagerViewModel>(
          builder: (_, read, child) {
            return Column(children: _getWidget(read.otherLayerList, read));
          },
        ),
      ],
    );
  }

  List<Widget> _getLayerWidget(
      List<NIRasterTileLayer> data, DrawerManagerViewModel read) {
    List<Widget> list = [];

    if (data.isNotEmpty) {
      for (int i = 0; i < data.length; i++) {
        list.add(__createGridViewItems(data[i], read));
      }
    }

    return list;
  }

  List<Widget> _getWidget(
      List<NIRasterTileLayer> data, DrawerManagerViewModel read) {
    List<Widget> list = [];

    if (data.isNotEmpty) {
      int calc = data.length % ConstantConfig.numSize;

      int num = calc == 0
          ? data.length ~/ ConstantConfig.numSize
          : data.length ~/ ConstantConfig.numSize + 1;

      for (int i = 0; i < num; i++) {
        List<NIRasterTileLayer> temp = [];

        for (int j = i * ConstantConfig.numSize;
            j < (i + 1) * ConstantConfig.numSize;
            j++) {
          if (j < data.length) {
            temp.add(data[j]);

            if (temp.length == ((i + 1) * ConstantConfig.numSize) ||
                j == data.length - 1) {
              list.add(Row(children: _getLayerWidget(temp, read)));
            }
          }
        }
      }
    }
    return list;
  }

  Widget __createGridViewItems(
      NIRasterTileLayer layer, DrawerManagerViewModel read) {
    return Expanded(
        flex: 1,
        child: Center(
            child: Container(
                padding: const EdgeInsets.all(ConstantConfig.appDefaultMargin),
                decoration: layer.isSelect == true
                    ? BoxDecoration(
                        border: Border.all(color: Colors.lightBlue, width: 2),
                        borderRadius: BorderRadius.circular((20.0)))
                    : null,
                child: Column(children: [
                  Stack(
                    children: [
                      GestureDetector(
                        child:
                            Image.asset(layer.imagePath!, fit: BoxFit.contain),
                        onTap: () {
                          read.switchNIRasterTileLayer(layer);
                        },
                      ),
                      read.isDelLayer == true && layer.isDel == true
                          ? Positioned(
                              right: 0,
                              top: ConstantConfig.appDefaultMargin,
                              child: GestureDetector(
                                child: const Icon(
                                  Icons.clear,
                                  color: Colors.red,
                                ),
                                onTap: () {
                                  read.removeLayer(layer);
                                },
                              ),
                            )
                          : const Text("")
                    ],
                  ),
                  Text(layer.title!),
                ]))));
  }

  @override
  void dispose() {}

  @override
  MapTitleBarAdapter createMapTitleBarAdapter(BuildContext context) {
    final DrawerManagerViewModel read = context.read<DrawerManagerViewModel>();
    return MapTitleBarAdapter(
      leading: BackButton(
        color: Colors.black,
        onPressed: () {
          read.closeDrawer();
        },
      ),
    );
  }

  @override
  void update() {}
}
